package cn.jdy.plugins.web;

import java.net.InetAddress;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.util.IpUtil;
import cn.jdy.plugins.entity.Comment;
import cn.jdy.plugins.service.CommentService;

/***
 * @desc   评论控制类
 * @author lph
 * @date   2016-8-16
 */
@Controller
@RequestMapping("/comment")
public class CommentCtrl extends BaseCtrl {
	@Autowired
	private CommentService commentservice;
	
	/***
	 * @author Administrator
	 * @param comment
	 * @desc  发布评论
	 */
	@RequestMapping(value = "/addComment", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addComment(Comment comment){
        Map<String, Object> jsonMap = new HashMap<String, Object>();
		
		Date dt=new Date();//如果不需要格式,可直接用dt,dt就是当前系统时间
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置显示格式
		String nowTime="";
		nowTime= df.format(dt);//用DateFormat的format()方法在dt中获取并以yyyy/MM/dd HH:mm:ss格式显示
		try {
			comment.setCreateTime(nowTime);
			comment.setStatus("0");
			commentservice.save(comment);
			jsonMap.put("status", "发布成功啦！");
		} catch (Exception e) {
			// TODO: handle exception
			BaseLog.e(this.getClass(), "addComment", e);
		}
		return jsonMap;
	}
	
	
	/***
	  * @desc  获取所有评论
	  * @author lph
	  */
	 @RequestMapping("/queryAllComment")
	 @ResponseBody
	 public Map<String, Object> queryAllComment(Comment comm){
		 Map<String, Object> jsonMap = new HashMap<String, Object>();
		 try {
			jsonMap.put("dataList", commentservice.querypage(comm));
			jsonMap.put("iTotalDisplayRecords",commentservice.count(comm));
		} catch (Exception e) {
			BaseLog.e(this.getClass(), "查询异常", e);
		}	 
		 return jsonMap; 
	 }
}
